import requests,time
from lxml import etree
import csv

class LcSpider(object):
    def __init__(self):
        self.url = 'http://lcdzsbsh.cn.biz72.com/'
        self.html = ''
        self.headers = {
            'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'
        }

    def get_html(self):
        time.sleep(1)
        response = requests.get(self.url, headers=self.headers).text
        # print(response)
        self.html = etree.HTML(response)


    def html_content(self,url):
        # response = requests.get(self.url,headers=self.headers).text
        # # print(response)
        # self.html = etree.HTML(response)

        urls = self.html.xpath('//div[@class="obj-relative-info-b clearfix"]//a/@href')
        companys = self.html.xpath('//div[@class="obj-relative-info-b clearfix"]//a/text()')
        # print(urls)
        for index,url in enumerate(urls):
            company = companys[index]
            print(company,url)


            with open('LeiChe.csv','a',encoding='gbk') as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow([f"{company}" ,f"{url}"])



    def run(self):
        context = LcSpider()
        context.get_html()
        context.html_content(url=self.url)



if __name__ == '__main__':
    hehuang = LcSpider()
    hehuang.run()

